package com.hutao.manage.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.manage.mapper.HzMapper;
import com.hutao.manage.pojo.RetrievingRecord;
import com.hutao.manage.service.HzService;

@Service
public class HzServiceImpl implements HzService{
	
	@Autowired
	private HzMapper hzMapper;

	
	@Transactional(readOnly = true)
	@Override
	public String handleDate(String inTime) {
		String startTime = inTime+" 00:00:00";
		String endTime = inTime+" 23:59:59";
		
		List<RetrievingRecord> retrievingRecords = new ArrayList<>();
		RetrievingRecord retrievingRecord = null;
		List<String> twiceIdCard = new ArrayList<>();
		int count = 0;
		List<Map<String,Object>> idcardMapList = hzMapper.getRepeatIdCard(inTime, startTime, endTime);
		for (Map<String, Object> map : idcardMapList) {
			String idCard = MapUtils.getString(map, "ID_CARD");
			List<Map<String,Object>> depMapList = hzMapper.getRepeatDept(inTime, startTime, endTime, idCard);
			if(CollectionUtils.isNotEmpty(depMapList)) {
				count++;
				
				for (Map<String, Object> depMap : depMapList) {
					String inDepaId = MapUtils.getString(depMap, "IN_DEPA_ID");
					
					List<Map<String,Object>> repeatDataList = hzMapper.getRepeatData(startTime, endTime, idCard, inDepaId);
					//{ONLY_ID=23685045, INACCONO=null, IN_TIME=oracle.sql.TIMESTAMP@55a384e1, DOCTOR_NAME=李红军, MR_ID=0002186027, IN_DEPA_ID=18C070024, ID=8825997, ID_CARD=370782198606265816, NAME=丁文桂}
					//{ONLY_ID=23685064, INACCONO=Y260013, IN_TIME=oracle.sql.TIMESTAMP@6c1c247b, DOCTOR_NAME=李红军, MR_ID=0002186027, IN_DEPA_ID=18C070024, ID=8826015, ID_CARD=370782198606265816, NAME=丁文桂}
					if(CollectionUtils.isEmpty(repeatDataList)) {
						continue;
					}
					retrievingRecord = new RetrievingRecord();
					List<String> retrievingOnlyIdList = new ArrayList<>();
					String firstOnlyId = null;
					int inAccoNoCount = 0;
					int repeatDataLength = repeatDataList.size();
					int length = 0;
					for (Map<String, Object> repeatData : repeatDataList) {
						String onlyId = MapUtils.getString(repeatData, "ONLY_ID");
						String inAccoNo = MapUtils.getString(repeatData, "INACCONO");
						//第二次交费不算回诊
						if(StringUtils.isNotEmpty(inAccoNo)) {
							inAccoNoCount++;
							if(inAccoNoCount>1) {
								twiceIdCard.add(idCard);
								continue;
							}
						}
						
						if(length==0) {
							firstOnlyId = onlyId;
						}else {
							retrievingOnlyIdList.add(onlyId);
						}
						
						length++;
					}
					//交费次数和就诊次数一致不算回诊
					if(repeatDataLength==inAccoNoCount) {
						continue;
					}
					
					count++;
					retrievingRecord.setOnlyId(firstOnlyId);
					retrievingRecord.setHzOnlyIdList(retrievingOnlyIdList);
					retrievingRecords.add(retrievingRecord);
					
				
					
				}
				
			}else {
				continue;
			}	
		}
		
		int handleCount = 0;
		
		for (RetrievingRecord record : retrievingRecords) {
			String onlyId = record.getOnlyId();
			List<String> retrievingList = record.getHzOnlyIdList();
			String retrievingStr = StringUtils.join(retrievingList.toArray(), ",");
			
			hzMapper.updateReturnVisitIdByOnlyId(onlyId, retrievingStr);
			for (String oldOnlyId : retrievingList) {
				hzMapper.updateStopFlagByOnlyId(oldOnlyId);
				hzMapper.updateDiagNossOnlyId(onlyId, oldOnlyId);
				//hzMapper.updateMrOutRecordOnlyId(onlyId, onlyId);
			}
			handleCount++;
			
			//hzMapper.updateReturnVisitIdByOnlyId(onlyId, returnVisitId)
		}
		
		
		System.out.println(twiceIdCard);
		
		
		System.out.println("总数为:"+retrievingRecords.size());
		return "处理会诊数据数量:"+handleCount;
	}
	
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("a");
		list.add("b");
		 System.out.println(StringUtils.join(list.toArray(), ","));

	
	}

}
